package com.thread.queue;

import java.util.Random;
import java.util.concurrent.PriorityBlockingQueue;

public class ProducerConsumer {
    private static final int QUEUE_CAPACITY = 10;
    private static final int PRODUCE_DELAY_MS = 1000;
    private static final int CONSUME_DELAY_MS = 500;

    public static void main(String[] args) {
        PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>(QUEUE_CAPACITY);

        Thread producer = new Thread(() -> {
            Random random = new Random();
            while (true) {
                try {
                    int num = random.nextInt(100);
                    queue.put(num);
                    System.out.println("Produced: " + num);
                    Thread.sleep(PRODUCE_DELAY_MS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread consumer = new Thread(() -> {
            while (true) {
                try {
                    int num = queue.take();
                    System.out.println("Consumed: " + num);
                    Thread.sleep(CONSUME_DELAY_MS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        producer.start();
        consumer.start();
    }
}

